home *** CD-ROM | disk | FTP | other *** search
/ MACD 5 / MACD 5.bin / workbench / libs / unixlib.lha / unix / test / socks / streamwrite.c < prev   
C/C++ Source or Header  |  1996-10-22  |  1KB  |  56 lines

  1. #include <proto/socket.h>
  2. #include <sys/types.h>
  3. #include <sys/socket.h>
  4. #include <netinet/in.h>
  5. #include <netdb.h>
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <unistd.h>
  10.  
  11. #define DATA "Half a league, half a league . . ."
  12.  
  13. /*
  14.  * This program creates a socket and initiates a connection with the socket
  15.  * given in the command line.  One message is sent over the connection and
  16.  * then the socket is closed, ending the connection. The form of the command
  17.  * line is streamwrite hostname portnumber 
  18.  */
  19.  
  20. int
  21. main(int argc, char **argv)
  22. {
  23.     int sock;
  24.     struct sockaddr_in server;
  25.     struct hostent *hp;
  26.  
  27.     if (argc < 3) {
  28.         fprintf(stderr, "Usage: %s <host> <port>\n", argv[0]);
  29.         exit(10);
  30.     }
  31.     /* Create socket */
  32.     sock = socket(AF_INET, SOCK_STREAM, 0);
  33.     if (sock < 0) {
  34.         perror("opening stream socket");
  35.         exit(10);
  36.     }
  37.     /* Connect socket using name specified by command line. */
  38.     server.sin_family = AF_INET;
  39.     hp = gethostbyname(argv[1]);
  40.     if (hp == 0) {
  41.         fprintf(stderr, "%s: unknown host\n", argv[1]);
  42.         exit(20);
  43.     }
  44.     bcopy(hp->h_addr, &server.sin_addr, hp->h_length);
  45.     server.sin_port = htons(atoi(argv[2]));
  46.  
  47.     if (connect(sock, (struct sockaddr *)&server, sizeof(server)) < 0) {
  48.         perror("connecting stream socket");
  49.         exit(10);
  50.     }
  51.     if (write(sock, DATA, sizeof(DATA)) < 0)
  52.         perror("writing on stream socket");
  53.     close(sock);
  54.     return 0;
  55. }
  56.